package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.manager.service.IContactAdminService;
import com.codedancer.campus.manager.service.bo.contactAdmin.ContactAdminChatMessagePageSearchBo;
import com.codedancer.campus.manager.service.bo.contactAdmin.ContactAdminChatRoomPageSearchBo;
import com.codedancer.campus.manager.service.vo.contactAdmin.ContactAdminChatMessagePageVo;
import com.codedancer.campus.manager.service.vo.contactAdmin.ContactAdminChatRoomPageVo;
import com.codedancer.campus.mapper.ContactAdminChatMessageMapper;
import com.codedancer.campus.mapper.ContactAdminChatRoomMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.ContactAdminChatMessage;
import com.codedancer.campus.po.ContactAdminChatRoom;
import com.codedancer.campus.po.User;
import com.codedancer.campus.qo.contactAdmin.ContactAdminChatMessagePageSearchQo;
import com.codedancer.campus.qo.contactAdmin.ContactAdminChatRoomPageSearchQo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContactAdminServiceImpl implements IContactAdminService {

    @Autowired
    private ContactAdminChatRoomMapper contactAdminChatRoomMapper;
    @Autowired
    private ContactAdminChatMessageMapper contactAdminChatMessageMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页获取联系管理员聊天室
     *
     * @param contactAdminChatRoomPageSearchBo
     * @return
     */
    @Override
    public IPage<ContactAdminChatRoomPageVo> pageChatRoom(ContactAdminChatRoomPageSearchBo contactAdminChatRoomPageSearchBo) {
        ContactAdminChatRoomPageSearchQo contactAdminChatRoomPageSearchQo = BeanUtil.copyProperties(contactAdminChatRoomPageSearchBo, ContactAdminChatRoomPageSearchQo.class);
        IPage<ContactAdminChatRoom> contactAdminChatRoomIPage = contactAdminChatRoomMapper.pageChatRoom(new Page<ContactAdminChatRoom>(contactAdminChatRoomPageSearchBo.getPage(), contactAdminChatRoomPageSearchBo.getSize()), contactAdminChatRoomPageSearchQo);
        List<ContactAdminChatRoomPageVo> contactAdminChatRoomPageVos;
        IPage<ContactAdminChatRoomPageVo> contactAdminChatRoomPageVoIPage = new Page<>();
        contactAdminChatRoomPageVos = contactAdminChatRoomIPage.getRecords().stream().map(contactAdminChatRoom -> {
            ContactAdminChatRoomPageVo contactAdminChatRoomPageVo = BeanUtil.copyProperties(contactAdminChatRoom, ContactAdminChatRoomPageVo.class);
            User user = userMapper.findById(contactAdminChatRoom.getUserUuid()).orElseThrow(() -> new BusinessException("用户不存在"));
            contactAdminChatRoomPageVo.setNickName(user.getNickName());
            contactAdminChatRoomPageVo.setUserAvatar(user.getAvatar());
            // 查询最近一次聊天信息
            ContactAdminChatMessage latestContactAdminChatMessage = contactAdminChatMessageMapper.latestChatMessageByRoomId(contactAdminChatRoom.getUuid());
            if (ObjectUtil.isNotEmpty(latestContactAdminChatMessage)) {
                contactAdminChatRoomPageVo.setLatestChatMessage(latestContactAdminChatMessage.getContent());
                contactAdminChatRoomPageVo.setLatestChatTime(latestContactAdminChatMessage.getCreateTime());
            }
            return contactAdminChatRoomPageVo;
        }).collect(Collectors.toList());
        contactAdminChatRoomPageVoIPage.setRecords(contactAdminChatRoomPageVos);
        return contactAdminChatRoomPageVoIPage;
    }

    /**
     * 获取聊天记录
     *
     * @param contactAdminChatMessagePageSearchBo
     * @return
     */
    @Override
    public IPage<ContactAdminChatMessagePageVo> pageHistory(ContactAdminChatMessagePageSearchBo contactAdminChatMessagePageSearchBo) {
        ContactAdminChatMessagePageSearchQo contactAdminChatMessagePageSearchQo = BeanUtil.copyProperties(contactAdminChatMessagePageSearchBo, ContactAdminChatMessagePageSearchQo.class);
        IPage<ContactAdminChatMessage> contactAdminChatMessageIPage = contactAdminChatMessageMapper.pageHistory(new Page<ContactAdminChatMessage>(contactAdminChatMessagePageSearchBo.getPage(), contactAdminChatMessagePageSearchBo.getSize()), contactAdminChatMessagePageSearchQo);
        List<ContactAdminChatMessagePageVo> contactAdminChatMessagePageVos;
        IPage<ContactAdminChatMessagePageVo> contactAdminChetMessagePageVoIPage = new Page<>();
        contactAdminChatMessagePageVos = contactAdminChatMessageIPage.getRecords().stream().map(contactAdminChatMessage -> {
            ContactAdminChatMessagePageVo contactAdminChatMessagePageVo = BeanUtil.copyProperties(contactAdminChatMessage, ContactAdminChatMessagePageVo.class);
            contactAdminChatMessagePageVo.setSendTime(contactAdminChatMessage.getCreateTime());
            if (ObjectUtil.isNotEmpty(contactAdminChatMessage.getSendUserUuid())) {
                User user = userMapper.findById(contactAdminChatMessage.getSendUserUuid()).orElseThrow(() -> new BusinessException("用户不存在"));
                if (ObjectUtil.isEmpty(user)) {
                    user = userMapper.findById(contactAdminChatMessage.getReceiveUserUuid()).orElseThrow(() -> new BusinessException("用户不存在"));
                }
                contactAdminChatMessagePageVo.setUserAvatar(user.getAvatar());
            }
            return contactAdminChatMessagePageVo;
        }).collect(Collectors.toList());
        contactAdminChetMessagePageVoIPage.setRecords(contactAdminChatMessagePageVos);
        contactAdminChetMessagePageVoIPage.setTotal(contactAdminChatMessageIPage.getTotal());
        return contactAdminChetMessagePageVoIPage;
    }
}
